package com.ruoyi.report.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.oiltank.domain.OilTank;
import com.ruoyi.oiltank.mapper.OilTankMapper;
import com.ruoyi.oiltank.service.IOilTankService;
import com.ruoyi.oiltank.service.impl.OilTankServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.report.domain.ReportOilDepotDetail;
import com.ruoyi.report.mapper.ReportOilDepotMapper;
import com.ruoyi.report.domain.ReportOilDepot;
import com.ruoyi.report.service.IReportOilDepotService;

/**
 * 油库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-15
 */
@Service
public class ReportOilDepotServiceImpl implements IReportOilDepotService 
{
    @Autowired
    private ReportOilDepotMapper reportOilDepotMapper;

    /**
     * 查询油库
     * 
     * @param id 油库ID
     * @return 油库
     */
    @Override
    public ReportOilDepot selectReportOilDepotById(Long id)
    {
        return reportOilDepotMapper.selectReportOilDepotById(id);
    }

    /**
     * 查询油库列表
     * 
     * @param reportOilDepot 油库
     * @return 油库
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ReportOilDepot> selectReportOilDepotList(ReportOilDepot reportOilDepot)
    {
        List<String> dateQuery = reportOilDepot.getDateQuery();
        if (CollectionUtil.isNotEmpty(dateQuery)){
            String start = dateQuery.get(0);
            String end = dateQuery.get(1);
            reportOilDepot.setStart(start);
            reportOilDepot.setEnd(end);
        }
        return reportOilDepotMapper.selectReportOilDepotList(reportOilDepot);
    }
    public Integer countTodayRecordsDepot( Long deptId){
        return reportOilDepotMapper.countTodayRecordsDepot(deptId);
    }
    /**
     * 新增油库
     * 
     * @param reportOilDepot 油库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReportOilDepot(ReportOilDepot reportOilDepot)
    {
        reportOilDepot.setCreateTime(DateUtils.getNowDate());
        int rows = reportOilDepotMapper.insertReportOilDepot(reportOilDepot);
        insertReportOilDepotDetail(reportOilDepot);

        List<ReportOilDepotDetail> reportOilDepotDetailList = reportOilDepot.getReportOilDepotDetailList();
        for (ReportOilDepotDetail reportOilDepotDetail : reportOilDepotDetailList) {
            Long tankId = reportOilDepotDetail.getTankId();
            BigDecimal tankHeight = reportOilDepotDetail.getTankHeight();
            Integer tankStatus = reportOilDepotDetail.getTankStatus();
            BigDecimal tankWeight = reportOilDepotDetail.getTankWeight();
            OilTank oilTank = oilTankService.selectOilTankById(tankId);
            if (ObjectUtil.isNull(oilTank)){
                throw new RuntimeException("查询不到储罐信息");
            }
            oilTank.setId(tankId);
            oilTank.setStatus(tankStatus);
            oilTank.setWeight(tankWeight);
            oilTank.setHeight(tankHeight);
            oilTankService.updateOilTank(oilTank);

        }


        return rows;
    }
@Autowired
private IOilTankService oilTankService;
    /**
     * 修改油库
     * 
     * @param reportOilDepot 油库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateReportOilDepot(ReportOilDepot reportOilDepot)
    {
        reportOilDepot.setUpdateTime(DateUtils.getNowDate());
        reportOilDepotMapper.deleteReportOilDepotDetailByDepotId(reportOilDepot.getId());
        insertReportOilDepotDetail(reportOilDepot);
        List<ReportOilDepotDetail> reportOilDepotDetailList = reportOilDepot.getReportOilDepotDetailList();
        for (ReportOilDepotDetail reportOilDepotDetail : reportOilDepotDetailList) {
            Long tankId = reportOilDepotDetail.getTankId();
            BigDecimal tankHeight = reportOilDepotDetail.getTankHeight();
            Integer tankStatus = reportOilDepotDetail.getTankStatus();
            BigDecimal tankWeight = reportOilDepotDetail.getTankWeight();
            OilTank oilTank = oilTankService.selectOilTankById(tankId);
            if (ObjectUtil.isNull(oilTank)){
                throw new RuntimeException("查询不到储罐信息");
            }
            oilTank.setId(tankId);
            oilTank.setStatus(tankStatus);
            oilTank.setWeight(tankWeight);
            oilTank.setHeight(tankHeight);
            oilTankService.updateOilTank(oilTank);

        }
        return reportOilDepotMapper.updateReportOilDepot(reportOilDepot);
    }

    /**
     * 批量删除油库
     * 
     * @param ids 需要删除的油库ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReportOilDepotByIds(Long[] ids)
    {
        reportOilDepotMapper.deleteReportOilDepotDetailByDepotIds(ids);
        return reportOilDepotMapper.deleteReportOilDepotByIds(ids);
    }

    /**
     * 删除油库信息
     * 
     * @param id 油库ID
     * @return 结果
     */
    @Override
    public int deleteReportOilDepotById(Long id)
    {
        reportOilDepotMapper.deleteReportOilDepotDetailByDepotId(id);
        return reportOilDepotMapper.deleteReportOilDepotById(id);
    }

    /**
     * 新增油罐信息详情信息
     * 
     * @param reportOilDepot 油库对象
     */
    public void insertReportOilDepotDetail(ReportOilDepot reportOilDepot)
    {
        List<ReportOilDepotDetail> reportOilDepotDetailList = reportOilDepot.getReportOilDepotDetailList();
        Long id = reportOilDepot.getId();
        if (StringUtils.isNotNull(reportOilDepotDetailList))
        {
            List<ReportOilDepotDetail> list = new ArrayList<ReportOilDepotDetail>();
            for (ReportOilDepotDetail reportOilDepotDetail : reportOilDepotDetailList)
            {
                reportOilDepotDetail.setDepotId(id);
                list.add(reportOilDepotDetail);
            }
            if (list.size() > 0)
            {
                reportOilDepotMapper.batchReportOilDepotDetail(list);
            }
        }
    }
}
